package com.reefe.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.reefe.config.ResourceConfig;
import com.reefe.dto.ContentDto;
import com.reefe.dto.front.AD1Node;
import com.reefe.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class PageController {
    @Autowired
    private ResourceConfig resourceConfig;
    @Reference(version = "1.0.0")
    private ContentService contentService;

    @RequestMapping("/")
    public String index(Model model){
        Map<String, Object> map = contentService.getContentListByCid(resourceConfig.getAD1_CATEGORY_ID());
        List<ContentDto> list = (List<ContentDto>) map.get("data");
        //将列表转换为AD1Node列表
        List<AD1Node> adList = new ArrayList<>();
        for(ContentDto content : list){
            AD1Node adNode = new AD1Node();
//            adNode.setAlt(content.getTitle());
            adNode.setHeight(resourceConfig.getAD1_HEIGHT());
            adNode.setHeightB(resourceConfig.getAD1_HEIGHT_B());
            adNode.setWidth(resourceConfig.getAD1_WIDTH());
            adNode.setWidthB(resourceConfig.getAD1_WIDTH_B());
            adNode.setSrc(content.getImage());
            adNode.setSrcB(content.getImage());
//            adNode.setHref(content.get);
            adList.add(adNode);
        }
        String ad1 = JSON.toJSONString(adList);
        model.addAttribute("ad1", ad1);
        return "index";
    }
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }
}